<?php
/**
 * im审核
 * Date: 2022/3/30
 * Author: sunlx
 */
namespace Frieza\Modules\Im\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\model\ImCheckList;
use Doraemon\service\Member as sMember;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;

class ImCheckController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        Admin::script('$(".pjax-container").attr("id","pjax-container");');
        Admin::script($this->addScript());
    }

    public function imgCheckList(Request $request, Content $content)
    {
        $params = $request->input();
        $pageSize = $request->input("per_page", 20);
        $page = $request->input("page", 1);

        $search = [];
        if (isset($params['send_time']) && $params['send_time']) {
            if ($params['send_time']['start']) {
                $search['send_time[>]'] = strtotime($params['send_time']['start']);
            }
            if ($params['send_time']['end']) {
                $search['send_time[<]'] = strtotime($params['send_time']['end']);
            }
        }
        if (isset($params['from_uid']) && $params['from_uid']) {
            $search['from_uid'] = $params['from_uid'];
        }
        $list = ImCheckList::getList($page, $pageSize, $search);

        $gridModel = new pGridDataModel($list['list'], $list['total'], [
            'perPage' => $pageSize
        ]);
        $gridList = Admin::grid($gridModel, function (Grid $grid) use ($gridModel) {
            $grid->paginate($gridModel->getPerPage());
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->disableCreateButton();

            $grid->filter(function($filter){
                $filter->disableIdFilter();
                $filter->column(1 / 2, function ($filter) {
                    $filter->equal('from_uid', '用户ID');
                });
                $filter->column(1 / 2, function ($filter) {
                    $filter->between('send_time', '私信时间')->datetime();
                });

                $filter->expand();
            });

            $grid->column('nick', '昵称')->display(function () {
                $userInfo = sMember::getUserInfoByUid($this->from_uid);
                return "<a href='/admin/user/Detail/$this->from_uid' target='_blank' >" . $userInfo['nick'] . "</a>";
            })->width(200);
            $grid->column('from_uid', '用户id');
            $grid->column('reason', '备注')->display(function ($reason) {
                $reason = json_decode($reason,true);
                $html = "<span style='display: inline-block;width:80px;height:80px;cursor: pointer;'><img class='xc-img' style='width:100%;height:100%;' src='{$this->content}' /></span>";
                $html .=  "&nbsp;&nbsp;违规原因：".implode('，', $reason);
                $html .= '<div id="img-modal" class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true"><div class="modal-dialog" style="max-width:90%;"><div class="modal-content"></div></div></div>';
                return $html . "</br>";
            });
            $grid->column("send_time", '私信时间')->display(function ($send_time) {
                return $send_time ? date("Y-m-d H:i:s", $send_time) : '';
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableEdit();
                $actions->disableDelete();
                $actions->append('<a href="javascript:;" data-id="'.$actions->row->id.'" class="fa fa-check sub-check"> 审核通过</a>');
            });
        });

        $tab = new Tab();
        //$tab->addLink('头像审核列表', url('admin/RiskContent/checkLog/list'));
        //$tab->addLink('背景图审核列表', url('admin/RiskContent/checkLog/backgroundList'));
        $tab->add('私信图审核列表', $gridList->render(),true);
        //$tab->addLink('昵称审核列表', url('admin/RiskContent/checkLog/nickList'));
        //$tab->addLink('简介审核列表', url('admin/RiskContent/checkLog/profileList'));

        return $content
            ->header('用户基础信息审核')
            ->breadcrumb(['text' => '用户基础信息审核'])
            ->row($tab);
    }

    public function imgCheck(Request $request)
    {
        try {
            $params = $request->all();
            unset($params['_token']);

            $result = ImCheckList::delIllegalImg($params['id'], Admin::user()->id);
            if ($result) {
                return response()->json([
                    'status' => true,
                    'message' => "审核成功",
                ]);
            } else {
                return response()->json([
                    'status' => false,
                    'message' => "审核失败",
                ]);
            }
        } catch (\Exception $e) {
            Log::debug($e->getMessage());
            return response()->json([
                'status' => false,
                'message' => $e->getMessage(),
            ]);
        }
    }

    private function addScript()
    {
        return <<<SCRIPT
            $('.sub-check').on('click',function(e){
                e.preventDefault();
                var id = $(this).attr("data-id"); 
                layer.confirm('确定要审核通过当前图片吗？', {
                    btn: ['确定', '取消']
                }, function (index) {
                    layer.close(index);
                    $.post("/admin/im/imgCheck", {id:id}, function(data){
                        layer.msg(data.message);
                        if (data.status) {
                            parent.window.location.reload();
                        }
                    });
                });
            });
            $('.xc-img').click(function(){
                var img_html = $(this).prop("outerHTML");
                $('#img-modal .modal-content').html(img_html);
                $('#img-modal').modal('show');
                // 点击隐藏 modal
                $('#img-modal .modal-content img').click(function(){
                    $('#img-modal').modal('hide');
                })
            });
SCRIPT;
    }

}